<?php
/*
Plugin Name: TravelPress
Plugin URI: http://www.travelsyndication.org/projects/travelpress
Description: Provides the ability to publish travel offers using the Travel Syndication Format (TSF)
Version: 1.0

Copyright 2008 The Travel Syndication Foundation (info@travelsyndication.org)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

include('tp-formatter.php');

wp_enqueue_script('jquery');

// TODO: temp!!!
wp_cache_flush();

add_action('admin_head', 'tp_head');
add_action('edit_form_advanced', 'tp_form_advanced');
add_action('save_post', 'tp_save_post');
add_action('delete_post', 'tp_delete_post');
add_action('do_feed_tsf-xml', 'tp_feed_xml');
add_action('do_feed_tsf-wsdl', 'tp_feed_wsdl');
add_action('do_feed_tsf-soap', 'tp_feed_soap');


//add_filter('user_can_richedit', 'tp_user_can_richedit');

function tp_head() {
	print '<link rel="stylesheet" href="' . get_bloginfo('wpurl') . '/wp-content/plugins/travelpress/includes/travelpress.css" type="text/css" />';
	//print '<script type="text/javascript" src="' . get_bloginfo('wpurl') . '/wp-content/plugins/travelpress/includes/jquery-1.2.3.min.js' . '"></script>';
	print '<script type="text/javascript" src="' . get_bloginfo('wpurl') . '/wp-content/plugins/travelpress/includes/travelpress.js' . '"></script>';
}

function tp_css() {
	include('tp-css.php');
}

function tp_form_advanced($id) {
	global $post_ID;
	global $wpdb;
	
	$table = $wpdb->prefix . 'tp_offers';
	$offer = $wpdb->get_row("SELECT * FROM $table WHERE ID = $post_ID");
	
	// new mode (not edit)
	if(empty($offer)) {
		$radio = array();
		$radios['offer_date_type_range'] = true;
		$radios['region_type_code'] = true;
		$radios['country_type_code'] = true;
		$radios['subdivision_type_code'] = true;
		$radios['city_type_code'] = true;
		$radios['standard_price_type_fixed'] = true;
		$radios['incentive_discount_price_type_fixed'] = true;
		$radios['adjusted_price_type_fixed'] = true;
		$radios['availability_datetime_type_fixed'] = true;
	}
	
	include('tp-form.php');
}

/*
 *	Called when a post is saved. If post contains an offer then insert it into the tp_offers table 
 *	and update the text of the actual post to include the offer data.
 *
 *	NOTE: This method was modeled after wp_insert_post in wordpress' post.php file.
 */
function tp_save_post($id) {
	global $wpdb;
	
	// export POST array as variables
	extract($_POST, EXTR_SKIP);
	
	$table = $wpdb->prefix . 'tp_offers';
	$offer = $wpdb->get_row("SELECT * FROM $table WHERE ID = $id");
	
	$update = false;
	if(!empty($offer)) {
		$update = true;
	}
	
	$formatter = new Formatter();
	if(!$update) {
		// insert offer
		$sql = "
		INSERT INTO $table (
			ID,
			summary_text ,
			restriction_text,
			restriction_minimum_age,
			restriction_maximum_age,
			restriction_residency_country_code,
			restriction_residency_subdivision_code,
			restriction_occupation_code,
			instruction_text,
			region_code,
			region_name,
			country_code,
			country_name,
			city_code,
			city_name,
			offer_fixed_date,
			offer_start_date,
			offer_end_date,
			availability_fixed_datetime,
			availability_start_datetime,
			availability_end_datetime,
			standard_price_fixed_amount,
			standard_price_minimum_amount,
			standard_price_maximum_amount,
			adjusted_price_fixed_amount,
			adjusted_price_from_amount,
			adjusted_price_to_amount,
			incentive_text,
			incentive_discount_percentage,
			incentive_discount_fixed_amount,
			incentive_discount_minimum_amount,
			incentive_discount_maximum_amount
		) VALUES (
			{$formatter->prepareColumnValue($id, 'numeric')},
			{$formatter->prepareColumnValue($summary_text, 'string')},
			{$formatter->prepareColumnValue($restriction_text, 'string')},
			{$formatter->prepareColumnValue($restriction_minimum_age, 'numeric')},
			{$formatter->prepareColumnValue($restriction_maximum_age, 'numeric')},
			{$formatter->prepareColumnValue($restriction_residency_country_code, 'string')},
			{$formatter->prepareColumnValue($restriction_residency_subdivision_code, 'string')},
			{$formatter->prepareColumnValue($restriction_occupation_code, 'string')},
			{$formatter->prepareColumnValue($instruction_text, 'string')},
			{$formatter->prepareColumnValue($region_code, 'string')},
			{$formatter->prepareColumnValue($region_name, 'string')},
			{$formatter->prepareColumnValue($country_code, 'string')},
			{$formatter->prepareColumnValue($country_name, 'string')},
			{$formatter->prepareColumnValue($city_code, 'string')},
			{$formatter->prepareColumnValue($city_name, 'string')},
			{$formatter->prepareColumnValue($offer_fixed_date, 'date')},
			{$formatter->prepareColumnValue($offer_start_date, 'date')},
			{$formatter->prepareColumnValue($offer_end_date, 'date')},
			{$formatter->prepareColumnValue($availability_fixed_datetime, 'datetime')},
			{$formatter->prepareColumnValue($availability_start_datetime, 'datetime')},
			{$formatter->prepareColumnValue($availability_end_datetime, 'datetime')},
			{$formatter->prepareColumnValue($standard_price_fixed_amount, 'numeric')},
			{$formatter->prepareColumnValue($standard_price_minimum_amount, 'numeric')},
			{$formatter->prepareColumnValue($standard_price_maximum_amount, 'numeric')},
			{$formatter->prepareColumnValue($adjusted_price_fixed_amount, 'numeric')},
			{$formatter->prepareColumnValue($adjusted_price_from_amount, 'numeric')},
			{$formatter->prepareColumnValue($adjusted_price_to_amount, 'numeric')},
			{$formatter->prepareColumnValue($incentive_text, 'string')},
			{$formatter->prepareColumnValue($incentive_discount_percentage, 'numeric')},
			{$formatter->prepareColumnValue($incentive_discount_fixed_amount, 'string')},
			{$formatter->prepareColumnValue($incentive_discount_minimum_amount, 'numeric')},
			{$formatter->prepareColumnValue($incentive_discount_maximum_amount, 'numeric')}
		);
		";
		$wpdb->query($sql);
	} else {
		// update offer
		$sql = "
		UPDATE $table SET 
			summary_text = {$formatter->prepareColumnValue($summary_text, 'string')}, 
			restriction_text = {$formatter->prepareColumnValue($restriction_text, 'string')},
			restriction_minimum_age = {$formatter->prepareColumnValue($restriction_minimum_age, 'numeric')}, 
			restriction_maximum_age = {$formatter->prepareColumnValue($restriction_maximum_age, 'numeric')}, 
			restriction_residency_country_code = {$formatter->prepareColumnValue($restriction_residency_country_code, 'string')}, 
			restriction_residency_subdivision_code = {$formatter->prepareColumnValue($restriction_residency_subdivision_code, 'string')}, 
			restriction_occupation_code = {$formatter->prepareColumnValue($restriction_occupation_code, 'string')}, 
			instruction_text = {$formatter->prepareColumnValue($instruction_text, 'string')}, 
			region_code = {$formatter->prepareColumnValue($region_code, 'string')}, 
			region_name = {$formatter->prepareColumnValue($region_name, 'string')}, 
			country_code = {$formatter->prepareColumnValue($country_code, 'string')}, 
			country_name = {$formatter->prepareColumnValue($country_name, 'string')}, 
			city_code = {$formatter->prepareColumnValue($city_code, 'string')}, 
			city_name = {$formatter->prepareColumnValue($city_name, 'string')}, 
			offer_fixed_date = {$formatter->prepareColumnValue($offer_fixed_date, 'date')}, 
			offer_start_date = {$formatter->prepareColumnValue($offer_start_date, 'date')}, 
			offer_end_date = {$formatter->prepareColumnValue($offer_end_date, 'date')}, 
			availability_fixed_datetime = {$formatter->prepareColumnValue($availability_fixed_datetime, 'datetime')}, 
			availability_start_datetime = {$formatter->prepareColumnValue($availability_start_datetime, 'datetime')}, 
			availability_end_datetime = {$formatter->prepareColumnValue($availability_end_datetime, 'datetime')}, 
			standard_price_fixed_amount = {$formatter->prepareColumnValue($standard_price_fixed_amount, 'numeric')}, 
			standard_price_minimum_amount = {$formatter->prepareColumnValue($standard_price_minimum_amount, 'numeric')}, 
			standard_price_maximum_amount = {$formatter->prepareColumnValue($standard_price_maximum_amount, 'numeric')}, 
			adjusted_price_fixed_amount = {$formatter->prepareColumnValue($adjusted_price_fixed_amount, 'numeric')}, 
			adjusted_price_from_amount = {$formatter->prepareColumnValue($adjusted_price_from_amount, 'numeric')}, 
			adjusted_price_to_amount = {$formatter->prepareColumnValue($adjusted_price_to_amount, 'numeric')}, 
			incentive_text = {$formatter->prepareColumnValue($incentive_text, 'string')}, 
			incentive_discount_percentage = {$formatter->prepareColumnValue($incentive_discount_percentage, 'numeric')},
			incentive_discount_fixed_amount = {$formatter->prepareColumnValue($incentive_discount_fixed_amount, 'numeric')},
			incentive_discount_minimum_amount = {$formatter->prepareColumnValue($incentive_discount_minimum_amount, 'numeric')},
			incentive_discount_maximum_amount = {$formatter->prepareColumnValue($incentive_discount_maximum_amount, 'nuermic')}
		WHERE ID = $id;";
		$wpdb->query($sql);
	}
	
	// update post
}

function tp_delete_post($id) {
	global $wpdb;
	$table = $wpdb->prefix . 'tp_offers';
	$wpdb->query("DELETE FROM $table WHERE ID = $id");
}

function tp_feed_xml() {
	print "<tsf>!</tsf>";
}

function tp_feed_wsdl() {
	print "<wsdl>ins</wsdl>";
}

function tp_feed_soap() {
	print "<soap>bubbles</soap>";
}

function tp_user_can_richedit() {
	return false;
}
?>